<template>
    <div>
        <div>
			用户名 : <input type="text" v-model="username" />
			</div>

			<br /><br />

			<div>
			密码 : <input type="password" v-model="password" />
			</div>
            <br /><br />

            <div>
				验证码 : <input type="text" v-model="code" />
			</div>

			<br /><br />

			<img @click="changeimg" :src="src" />
            <br>

            <Button @click="submit">登录</Button>&nbsp;&nbsp;&nbsp;

        <img @click="sina" src="http://localhost:8000/static/download.png" />
        
    </div>
</template>
<script>
export default {
    data() {
        return {
            username:'',
            password:'',
            code:'',
            src:'http://localhost:8000/code/'
            
        }
    },
    methods: {
        //点击切换验证码
	changeimg:function(){

		//定义随机数
		var num = Math.ceil(Math.random()*10);
		//将图片刷新
		this.src = this.src + "?code=" + num;

	},
        //微博登录
	sina:function(){
		
		//组装url
		let url = "https://api.weibo.com/oauth2/authorize?client_id=543875374&redirect_uri=http://127.0.0.1:8000/md_admin/weibo"
		//跳转
		window.location.href = url;

    },
    //提交事件
	submit:function(){


		//判断
		if(this.username == ''){

			//alert('用户名不能为空');
			this.$Message('用户名不能为空');
			return false;
		}

		if(this.password == ''){

		alert('密码不能为空');
		return false;
		}


		//请求后台接口
		this.axios.get('http://localhost:8000/login/',{params:{username:this.username,password:this.password,code:this.code}}).then((result) =>{

			console.log(result);
			this.$Message(result.data.message);

			//判断登录
			if(result.data.code == 200){

				//存储用户信息
				localStorage.setItem('username',result.data.username)
				localStorage.setItem('uid',result.data.uid)

				//跳转页面
				// this.$router.push('/');


			}else{

				this.$Message(result.data.message);

			}



        });
    },
    },
}
</script>